﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using Linkmeme.Controllers;

namespace Linkmeme.Views.Source
{
    public partial class ViewAll : ViewPage
    {
        protected string pageTitle = "";

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            StringBuilder sbOutput = new StringBuilder();

            SourceViewData<Picture> svdPic = this.ViewData["data"] as SourceViewData<Picture>;
            if (svdPic != null)
            {
                foreach (GroupItemData<Picture> item in svdPic.GroupData())
                {
                    sbOutput.Append(WriteDataList(svdPic.SourceType, item.GroupKey.ToCH(), item.Items));
                    groupactionlist.InnerHtml = WriteGroupActionLink(svdPic.SourceType);
                }

                pageTitle = "照片";
            }

            SourceViewData<Blog> svdBlog = this.ViewData["data"] as SourceViewData<Blog>;
            if (svdBlog != null)
            {
                foreach (GroupItemData<Blog> item in svdBlog.GroupData())
                {
                    sbOutput.Append(WriteDataList(svdBlog.SourceType, item.GroupKey.ToCH(), item.Items));
                    groupactionlist.InnerHtml = WriteGroupActionLink(svdBlog.SourceType);
                }
                pageTitle = "博客";
            }

            SourceViewData<Bookmark> svdBookmark = this.ViewData["data"] as SourceViewData<Bookmark>;
            if (svdBookmark != null)
            {
                foreach (GroupItemData<Bookmark> item in svdBookmark.GroupData())
                {
                    sbOutput.Append(WriteDataList(svdBookmark.SourceType, item.GroupKey.ToCH(), item.Items));
                    groupactionlist.InnerHtml = WriteGroupActionLink(svdBookmark.SourceType);
                }
                pageTitle = "书签";
            }

            SourceViewData<Status> svdStatus = this.ViewData["data"] as SourceViewData<Status>;
            if (svdStatus != null)
            {
                foreach (GroupItemData<Status> item in svdStatus.GroupData())
                {
                    sbOutput.Append(WriteDataList(svdStatus.SourceType, item.GroupKey.ToCH(), item.Items));
                    groupactionlist.InnerHtml = WriteGroupActionLink(svdStatus.SourceType);
                }
                pageTitle = "状态";
            }

            contentlist.InnerHtml = sbOutput.ToString();

        }

        private string WriteDataList(string sourceType, string key, object items)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<div id='maincontent'>");
            sb.Append("    <div class='mainblock'>");
            sb.Append("      <div class='btitle'>");
            sb.Append("        <h4>").Append(key).Append("</h4>");
            sb.Append("      </div>");
            sb.Append(Html.RenderUserControl(string.Format("~/Views/Shared/{0}List.ascx", sourceType), items, new { ItemCount = 4, IsGroupItem = true }));
            sb.Append("    <br />");
            sb.Append("    </div>");
            sb.Append("  </div>");

            return sb.ToString();
        }

        private string WriteGroupActionLink(string sourceType)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(Html.ActionLink<SourceController>(c => c.ViewAll(sourceType, "UserName"), "用户名")).Append(" | ");
            sb.Append(Html.ActionLink<SourceController>(c => c.ViewAll(sourceType, "PubDate"), "发布日期")).Append(" | ");
            sb.Append(Html.ActionLink<SourceController>(c => c.ViewAll(sourceType, "Provider"), "来源"));

            return sb.ToString();
        }

        protected string WriteFriendList()
        {
            StringBuilder sb = new StringBuilder();
            if (ViewData["friends"] != null)
            {
                List<Linkmeme.Models.User> friends = (List<Linkmeme.Models.User>)ViewData["friends"];
                friends.ForEach(f=> sb.Append(f.UserName).Append(" | "));
            }
            string s = sb.ToString();
            return s.Remove(s.Length - 2);
        }
    }
}
